package algorithm.子数组达到规定累加和的最大长度系列问题.转圈问题;

public class RotateMatrix {

    public static void rotate(int[][] matrix){
        int size = matrix.length / 2;
        for (int i = 0; i < size; i++) {
            rotateEdge(matrix, i);
        }
    }

    public static void rotateEdge(int[][] matrix, int index){
        int length = matrix.length - 2 * index;
        int index1 = index;
        int index2 = index1 + length - 1;
        for (int i = 0; i < length - 1; i++) {
            int tmp = matrix[index1][index1 + i];
            matrix[index1][index1 + i] = matrix[index2 - i][index1];
            matrix[index2 - i][index1] = matrix[index2][index2 - i];
            matrix[index2][index2 - i] = matrix[index1 + i][index2];
            matrix[index1 + i][index2] = tmp;

        }
    }

    public static void printMatrix(int[][] matrix) {
        for (int i = 0; i != matrix.length; i++) {
            for (int j = 0; j != matrix[0].length; j++) {
                System.out.print(matrix[i][j] + " ");
            }
            System.out.println();
        }
    }

    public static void main(String[] args) {
        int[][] matrix = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 } };
        printMatrix(matrix);
        rotate(matrix);
        System.out.println("=========");
        printMatrix(matrix);

    }

}
